﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using eVentasUpdater;

namespace CMD_DKT.Utilities
{
    public class Utilities
    {
        public static string[] CheckPathAccess(string path, Boolean showMessage)
        {
            string[] access = new string[2];
            string message="";

            try
            {
                System.IO.Directory.EnumerateFiles(path);
                access[0] = "1";
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(DirectoryNotFoundException))
                    message = "Directorio " + path + " no encontrado.";
                else
                {
                    if (ex.GetType() == typeof(UnauthorizedAccessException))
                    {
                        message = "Usted no tiene permisos suficientes para ejecutar esta aplicación";
                        MessageBox.Show(message, "Atención", MessageBoxButton.OK, MessageBoxImage.Stop);
                        UpdateProc.EndEVentasProcess();
                    }
                    else message = ex.Message;
                }

                access[0] = "0";
                access[1] = message;
            }

            if(access[0] == "0" && showMessage)
                MessageBox.Show(message, "Atención", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            return access;

        
        }//////

        public static string[] GetActualMethod()
        {
            try 
	        {	        
		        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
                System.Diagnostics.StackFrame sf = st.GetFrame(1);
                string baseMethod = sf.GetMethod().Name;
                string actualMethod = st.GetFrame(0).GetMethod().Name;

                string[] methods = new string[2] { baseMethod, actualMethod};

                return methods;
	        }
	        catch (Exception ex)
	        {		
		        throw ex;
	        }
            
        }

        public static bool checkPathToWrite(string path)
        {
            try
            {
                using (FileStream fs = File.Create(Path.Combine(path,
                 "AccessTemp.txt"), 1, FileOptions.DeleteOnClose))
                {
                    fs.Close();
                    return true;
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show("Usted no tiene permisos suficientes para ejecutar esta aplicación", "Atención", MessageBoxButton.OK, MessageBoxImage.Stop);
                UpdateProc.EndEVentasProcess();
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }//end class


}
